How do I do this bounce on irregular surface

The hit point on the surface

The function hitTest only tell us that the ball is "within" the bounding of the wall object. While the ball goes with a speed of 5 pixels per frame, hitTest only tell us that "Here is in the Wall, and 5 pixel out is Out the wall". But where is the wall ? I mean "surface hit point" ?

Well, we can move the ball back a bit to see whether the hitTest is still positive. If not, continue to move the ball back a bit, etc etc, until the hitTest is false. That is very near the surface point. But, how much is "a bit" ? If we take 0.1 pixel as "a bit", then we might need to move the ball 50 times to get the hitTest false point when the speed of the ball is 5/frame;

The other way is the technique of "AVERAGE POINT". If x=0 is Out, and x=5 is In, we test the average point x=2.5; If x=2.5 is Out, then we are going to take the average point of one In and one Out, that is (2.5+5)/2; In condition that x=2.5 is In, then we are going to test the average point (0+2.5)/2. Just 5 pass of the "test average point", we will get an accuracy of 5/Math.pow(2,5); It is faster than "move a bit and test" method.

The tangential angle for bounce

What is the reflection angle after bounce. We need to calculate the angle of  the tangential line of the surface. I pick 2 neibough surface point by method described above. Connecting these two neibough points result in a tangential line. That is the orientation of the "Wall" the ball is going to bounce against.

The bugs:

1. Penetrating the wall, escape from detection by hitTest

If the wall is thin, then the possibility occurrs that x=0 is Out and x=5 is also Out. The ball penetrating the wall which is at x=3 without detected by hitTest. No way to get complete solution. I just make the speed (pixel/frame) as low as possible and increase the frames/sec of the movie. So, this bug would be eliminated.

2. Stuck or entraped in the deep of the wall

This is a common problem. A ball hits the wall, gets bounce and gets a new _x and _y. What if the new position is still hitTest true ? I failed to re-produce this complication. But, I believe this is why the ball stuck or entrapped within the wall. In this condition, the ball triger "bounce" function every frame, because of hitTest always positive, so it just goes to and fro between 0-5; I tried to avoid this. I make my bounce only occurrs when the ball is originally Out.

3. Bounce not smooth

There is always a trivial imperfection in every "ball bounce" movie. Because our movie repaints the ball according with frame rate. We rarely see a ball "hit" right on the surface of the wall. The ball always a little off the wall or a little deep into the wall before bounce. However, the ball goes with smooth speed. Here, in my movie, I like to make the ball "hit" on the surface. So, the new position of the ball after bounce is at the surface not ball._x+=dx; This result is an abrupt stop when hit. Anyway, I like it.

¡@